﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

using Cisoft.Cisoft3G.BLL;
using Cisoft.Cisoft3G.Model;

public partial class Business_Cisoft3G_ContactsManage_DefineGroup : WebUI.Page.BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            ltScript.Text = "<script type=\"text/javascript\" src=\"../../../js/Basic/BasicPage.js\"></script><script type=\"text/javascript\" src=\"../../../js/My97DatePicker/WdatePicker.js\"></script>";
            chcAll.Attributes.Add("onclick", "SelectAllCheckboxes(this)");
            Bind();
        }
    }

    #region 数据绑定
    /// <summary>
    /// 数据绑定
    /// </summary>
    private void Bind()
    {
        //当前页码
        int cpn = AspNetPager1.CurrentPageIndex;
        //获取数据
        DataTable dt = new Cisoft.Cisoft3G.BLL.DefineGroupBO().GetListWithPage(cpn, 10, "").Tables[0];
        int pageNum = dt.Rows.Count;
        if (dt.Rows.Count > 0)
        {
            Repeater1.DataSource = dt;
            Repeater1.DataBind();
            AspNetPager1.RecordCount = new Cisoft.Cisoft3G.BLL.DefineGroupBO().GetListCount(cpn, 10, "");
            AspNetPager1.PageSize = 10;
        }
        else
        {
            Repeater1.DataSource = dt;
            Repeater1.DataBind();
            AspNetPager1.RecordCount = new Cisoft.Cisoft3G.BLL.DefineGroupBO().GetListCount(cpn, 10, "");
        }
    }
    #endregion

    #region 翻页事件
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        Bind();
    }
    #endregion

    #region 查询事件
    /// <summary>
    /// 查询事件
    /// </summary>
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        Bind();
    }
    #endregion

    #region 删除事件
    /// <summary>
    /// 删除事件
    /// </summary>
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        List<int> list = new List<int>();
        foreach (RepeaterItem item in Repeater1.Items)
        {
            CheckBox cbk_single = item.FindControl("cbk_single") as CheckBox;
            if (cbk_single.Checked)
            {
                Label lblContactID = item.FindControl("lblContactID") as Label;
                if (!string.IsNullOrEmpty(lblContactID.Text))
                {
                    list.Add(int.Parse(lblContactID.Text));
                }
            }
        }

        DefineGroupBO bll = new DefineGroupBO();
        string strError = string.Empty;
        if (list.Count > 0)
        {
            try
            {
                foreach (int id in list)
                {
                    bll.Delete(id);
                }
            }
            catch (Exception ex)
            {
                strError = ex.Message;
            }
            if (string.IsNullOrEmpty(strError))
            {
                PageRight("删除成功！", "DefineGroup.aspx", "返回 ", "");
            }
            else
            {
                PageError("删除失败，原因如下：" + strError + "。", Request.RawUrl);
            }
        }
    }
    #endregion 

    #region 详细事件
    protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "Edit")
        {
            Response.Redirect("DefineGroupEdit.aspx?id=" + e.CommandArgument );
        }
    }
    #endregion

    #region 新增事件
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Response.Redirect("DefineGroupAdd.aspx");
    }
    #endregion

    #region 显示组成员
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        Label lblDGMember = e.Item.FindControl("lblDGMember") as Label;
        ContactsBO bll = new ContactsBO();
        ContactsMO model = new ContactsMO();
        string members = string.Empty;
        if (e.Item.ItemType == ListItemType.Item)
        {
            string member = lblDGMember.Text;
            if (!string.IsNullOrEmpty(member))
            {
                string[] strs = member.TrimEnd(',').Split(',');
                if (strs.Length > 0)
                {
                    foreach (string s in strs)
                    {
                        model = bll.GetModel(int.Parse(s));
                        if (model != null)
                        {
                            members += string.Format("{0}({1}),",model.CT_Name,model.CT_Mobile);
                        }
                    }
                    members = members.TrimEnd(',');
                    if (members.Length >= 40)
                    {
                        lblDGMember.Text = members.Substring(0,37) + "...";
                        lblDGMember.ToolTip = members;
                    }
                    else
                    {
                        lblDGMember.Text = members;
                        lblDGMember.ToolTip = members;
                    }
                }
            }
            
        }
    }
    #endregion
}
